/** 
 * 七种 原始数据类型
 * undefined null Boolean Number Object String Symbol 
 */
// 独一无二, 不是对象，不是构造函数
let s1 = Symbol()
let s2 = Symbol()
console.log(s1 === s2)

// 声明多少就存在多少
let s3 = Symbol('foo')

// 对象 toString
const obj = {
  name: 'aqie',
  toString(){
    return this.name
  }
}

let s4 = Symbol(obj)
console.log(s4, s4.description, s3.description)

// 这种声明相当于定义在全局环境, 只存在一个, 没有作用域限制
let s5 = Symbol.for("aqie")
let s6 = Symbol.for("aqie")
console.log("Symbol.for", s5 === s6)

console.log('返回已经全局登记', Symbol.keyFor(s1), Symbol.keyFor(s6))

/** 
 * 应用场景: 
 * 1.班级人名重复
 * 2.保护变量
 * 3. 消除魔术字符串
 */

 // 1.
 // 对象key 可以是变量
const student1 = '王五'
const student2 = '王五'
const student3 = Symbol('赵六')
const student4 = Symbol('赵六')

// key 唯一
const grade = {
  张三: {address:'xx', phone:123},
  李四: {address:'', phone:123},
  张三: {address:'yy', phone:1234},
  [student1]: {address:'1', phone:1234},
  [student2]: {address:'2', phone:1234},
  [student3]: {address:'3', phone:1234},
  [student4]: {address:'4', phone:1234},
}
console.log(grade)
console.log(grade.student4, grade[student4])

// 2.
const sym = Symbol('aqie')
class People {
  constructor(name){
    this.name = name
    this[sym] = 'aqie.com'
  }
  getName(){
   return this.name + this[sym]
  }
}

let p = new People('啊切')
console.log(p, p.getName())

// for in 无法遍历 Symbol属性
for(let key in p){
  console.log(key)
}

for(let key of Object.keys(p)){
  console.log(key)
}

// 只取Symbol
for(let key of Object.getOwnPropertySymbols(p)){
  console.log(key)
}

// 全取
for(let key of Reflect.ownKeys(p)){
  console.log(key)
}

//3.
const shapeType = {
  triangle: Symbol(),
  circle: Symbol()
}
function getArea(shape){
  let area = 0
  switch(shape){
    case shapeType.triangle:
      area = 1
      break
    case shapeType.circle:
      area = 2
      break
  }
  return area
}

console.log(getArea(shapeType.circle))
